package com.wjw.java.io.reader_;

import java.io.FileReader;
import java.io.IOException;

/**
 * 字符输入流InputStreamReader之：
 * 文件输入流FileReader
 */
public class FileReader_ {

    public static void main(String[] args) {
        readChar();
        System.out.println("\n---------------");
        readCharBuff();
    }

    /**
     * 1.读取文件中的内容
     */
    public static void  readChar(){
        String filePath = "f:\\IO\\test_reader.txt";
        FileReader reader = null;
        int readData = 0;
        try {
            reader = new FileReader(filePath);
            while ((readData = reader.read()) != -1){
                System.out.print((char) readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 2.读取文件中的内容
     * 使用字符数组读取,一次可以读取多个字节,提高效率
     */
    public static void  readCharBuff(){
        String filePath = "f:\\IO\\test_reader.txt";
        FileReader reader = null;
        char [] buff = new char[64];
        int readLength = 0;
        try {
            reader = new FileReader(filePath);
            while ((readLength = reader.read(buff)) != -1){
                String result = new String(buff,0,readLength);
                System.out.println(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
